﻿using FileEncryption;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace FileEncryption.Test
{
    [TestClass()]
    public class FileEncryptionTest
    {
        [TestMethod()]
        [DeploymentItem("FileEncryption.exe")]
        public void MainTest()
        {
            int BUFFER_SIZE = 8 * 1024;
            FileEncryption.inputFile = "../../../FileEncryption/example.bin";
            string exampleFile = "../../../FileEncryption/example - Copy.bin";
            FileEncryption_Accessor.Main();
            FileStream input = new FileStream(FileEncryption.inputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            FileStream example = new FileStream(exampleFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            using (input)
            {
                using (example)
                {
                    byte[] expected = new byte[BUFFER_SIZE];
                    byte[] actual = new byte[BUFFER_SIZE];
                    while (true)
                    {
                        int bytesRead = input.Read(expected, 0, expected.Length);
                        if (bytesRead == 0)
                        {
                            // End of file reached
                            break;
                        }
                        bytesRead = example.Read(actual, 0, actual.Length);
                        if (bytesRead == 0)
                        {
                            // End of file reached
                            break;
                        }
                        for (int i = 0; i < BUFFER_SIZE; i++)
                        {
                            Assert.AreEqual(expected[i], actual[i]);
                        }
                    }
                }
            }
        }
                
        //[TestMethod()]
        //[DeploymentItem("FileEncryption.exe")]
        //public void DecryptFileTest()
        //{
        //    FileEncryption_Accessor.DecryptFile();
        //    Assert.Inconclusive("A method that does not return a value cannot be verified.");
        //}
        
        //[TestMethod()]
        //[DeploymentItem("FileEncryption.exe")]
        //public void EncryptFileTest()
        //{
        //    FileEncryption_Accessor.EncryptFile();
        //    Assert.Inconclusive("A method that does not return a value cannot be verified.");
        //}

        [TestMethod()]
        [DeploymentItem("FileEncryption.exe")]
        public void RotateBytesTest()
        {
            byte[] buf = { 15, 23, 56, 78, 19 };
            byte[] expected = { 19, 15, 23, 56, 78 };
            byte[] actual;
            actual = FileEncryption_Accessor.RotateBytes(buf);
            for (int i = 0; i < buf.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }
        }

        [TestMethod()]
        [DeploymentItem("FileEncryption.exe")]
        public void RotateBytesBackwardsTest()
        {
            byte[] buf = { 15, 23, 56, 78, 19 };
            byte[] expected = { 23, 56, 78, 19, 15 };
            byte[] actual;
            actual = FileEncryption_Accessor.RotateBytesBackwards(buf);
            for (int i = 0; i < buf.Length; i++)
            {
                Assert.AreEqual(expected[i], actual[i]);
            }
        }
    }
}
